//
//  ZYZWelcomeController.swift
//  MyWeiBo
//
//  Created by 牛牛很牛 on 15/12/17.
//  Copyright © 2015年 曾勇召. All rights reserved.
//

import UIKit
import SDWebImage

class ZYZWelcomeController: UIViewController {
    
    private var iconViewBottomConstrait : NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        
        //加载用户头像
        ZYZUserAcount.loadUserAccount()?.loadUserInfo({ (error) -> () in
            //判断是否有错误
            if error != nil{
            print("获取用户信息失败")
            }
            //获取成功，加载头像
            if let avatar_large =  ZYZUserAcount.loadUserAccount()?.avatar_large{
                self.iconImageView.sd_setImageWithURL(NSURL(string: avatar_large)!, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        })
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconViewBottomConstrait?.constant = -(UIScreen.mainScreen().bounds.height - 160)
      UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
        //self.iconViewBottomConstrait?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        //约束改变后，重新刷新界面
        self.view.layoutIfNeeded()

        }) { (_) -> Void in
            self.welcomeLabel.alpha = 0
            UIView.animateWithDuration(0.5, animations: { () -> Void in
                self.welcomeLabel.alpha = 1
                self.welcomeLabel.hidden = false
                }, completion: { (_) -> Void in
                    //动画完成，切换控制器到tabBar控制器
                   (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
            })
        }
        
    }
    
    
    private func prepareUI(){
        self.view.addSubview(bgImageView)
        self.view.addSubview(iconImageView)
        self.view.addSubview(welcomeLabel)
        
        bgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        //添加约束
        //背景图
        self.view.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        //图标
        self.view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        iconViewBottomConstrait = NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        self.view.addConstraint(iconViewBottomConstrait!)
        self.view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        self.view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        // label
        self.view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        



        
    
    }
    
    //MARK: - 懒加载
    lazy var bgImageView = UIImageView(image: UIImage(named: "ad_background"))
    lazy var iconImageView : UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = iconView.bounds.width * 0.5
        iconView.layer.masksToBounds = true
        
        
        return iconView
    
    }()
    
    lazy var welcomeLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "小妞,欢迎回来"
        label.hidden = true
        return label
    }()
    

}
